//
//  AppDelegate.swift
//  Weibo
//
//  Created by FF on 16/11/27.
//  Copyright © 2016年 Cold Wind. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        //sleep(3)
        //window = UIWindow()
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController()
        //window?.rootViewController = WBHomeViewController()
        //设置 window 为主窗口并显示出来
        window?.makeKeyAndVisible()
        loadAppinfo()
        return true
    }

}
// MARK: - 从服务器获取加载应用程序信息
extension AppDelegate {
    fileprivate func loadAppinfo() {
        //模拟异步
        DispatchQueue.global().async {
            // 1> 获取URL
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            // 2> data
            let data = NSData(contentsOf: url!)
            // 3> 写入磁盘
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]//-- 获取沙盒目录
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            // 直接保存在沙盒，等待下一次程序启动调用
            data?.write(toFile: jsonPath, atomically: true)
            print("应用程序加载完毕\(jsonPath)")
        }

    }
}

